<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layui-form layuimini-form layui-form-pane">
    <div class="layui-form-item">
        <input type="hidden" name="member_code" value="{$memberCode}">
        <input type="hidden" name="source" value="-1">
        <label class="layui-form-label required">选择卡券</label>
        <div class="layui-input-inline">
            <input type="hidden" name="voucher_id" id="voucherId" lay-verify="required">
            <input type="hidden" name="voucher_title" id="voucherTitle" lay-verify="required">
            <input type="hidden" name="price" id="voucherPrice" lay-verify="required">
            <input type="text" placeholder="请选择卡券" autocomplete="off" readonly class="layui-input" id="voucher_id">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">有效期</label>
        <div class="layui-input-inline">
            <input type="number" placeholder="请输入有效期" name="expiration" lay-verify="required" autocomplete="off"  class="layui-input">
        </div>
        <tip>有效期单位 : 天</tip>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">数量</label>
        <div class="layui-input-inline">
            <input type="number" placeholder="请输入赠送数量" name="num" lay-verify="required" autocomplete="off"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>

        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    layui.use(['form', 'tableSelect', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer,
            tableSelect = layui.tableSelect,
            $ = layui.$;
        /*选择卡券*/
        tableSelect.render({
            elem: '#voucher_id',
            searchKey: 'title',
            checkedKey: 'id',
            searchPlaceholder: '名称搜索',
            table: {
                url: '/admin/Member/giveVoucher?action=voucher',
                method: 'post',
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.data, //解析数据列表
                    };
                },
                cols: [[
                    {type: 'radio'},
                    {field: 'title', title: '名称', sort: true},
                    {field: 'price', title: '价值', sort: true},
                    {field: 'explain', title: '描述', sort: true},
                ]],
                page: true,
                limits: [15, 20, 25, 30, 50, 100],
                limit: 15
            },
            done: function (elem, data) {
                var NEWJSON = [];
                var voucherId = [];
                var voucher_title = [];
                var voucher_price = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.title)
                    voucherId.push(item.id)
                    voucher_title.push(item.title)
                    voucher_price.push(item.price)
                })
                elem.val(NEWJSON.join(","))
                $('#voucherId').val(voucherId.join(","))
                $('#voucherTitle').val(voucher_title.join(","))
                $('#voucherPrice').val(voucher_price.join(","))
            }
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: '/admin/Member/giveVoucher?action=ajax',
                type: 'post',
                dataType: 'json',
                data: data.field,
                success: function (res) {
                    if (res.status == true) {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                        window.parent.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function () {
                    layer.msg("报错了");
                }
            });

            return false;
        });

    });
</script>
</body>
</html>